package com.macrosolution.exdebito

import grails.converters.JSON
import grails.plugin.springsecurity.annotation.Secured;

/**
 * ProvinceController
 * A controller class handles incoming web requests and performs actions such as redirects, rendering views and so on.
 */
@Secured(['permitAll'])
class ProvinceController {

	static scaffold = true
//	def index = { }
	
	def list={
		
			def listProvincia = Provincia.findAllByNomeIlikeOrSiglaIlike('%'+params.index+'%','%'+params.index+'%');
			String result='{"results":[';
			if(listProvincia){
				int index=0;
				listProvincia.each {provincia->
					if(index>0){
						result+=','
					}
					result+='{"id":'+provincia.id+',"text":"'+((provincia.sigla)?provincia.nome+' ('+provincia.sigla+')':provincia.nome)+'"}'
					index++;
				}
			} 
			
			result+=']}'
			render JSON.parse(result) as JSON	
		
	}
	def get_municipality(){
		String result=''
		if(params.pr){
			long pr_id=params.getLong('pr')
			def provincia =Provincia.get(pr_id)
			def listComuni = Comune.findAllByProvincia(provincia);
			if(listComuni ){
				listComuni.each {comune->
					result+='<option  value="'+comune.id+'">'+comune.nome+'</option>'
				}
			}
		}
		render result
//		'<option id="2" value="ALATRI">ALATRI</option><option id="3" value="VITERBO">VITERBO</option>>'
	}
	
	def get_cap(){
		String cap='ND'
		Comune com;
		if(params.mun){
			com=Comune.get(params.getLong('mun'));
			if(com.cap){
				cap=com.cap
			}else{
				 
			}
		}
		
		render cap
	}
	
	
	private get_cap_google(String address){
		
	}
}
